package com.hf.action;


import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Query;

import tool.TimeTool;
import tool.UploadImg;

import com.base.bean.*;
import com.base.dao.*;
import com.base.service.*;

import com.base.domain.*;
import com.hf.bean.ActivityCount;

/**
 * 行动管理
 * @author Administrator
 *
 */

public class ActvtAction extends BaseAction{
	private Integer cid,type,oppType;	//行动类型
	private String longitude,latitude,memo,name;
	private String imgIds;	//上传到服务器的image的id 以逗号分割 最多3张图？
	private ClientBase cb;
	private Integer monthTotal;

	
	private List<Activity> actvtList;	
	private ClientDetail cd;
	private String state;	//state:a 行动新增   b：行动提交   x:share
	
	private ActivityCount actvCount;	// 行动统计类

	
	public ActvtAction() {

	}

//	按用户id统计各种行动类型的月总数
	public String getActivityCount(){
		actvCount = new ActivityCount();
		this.openId=(this.openId==null?"":this.openId);
		User user=UserService.getInstance().getByOpenId(this.openId);
		
		
		
		return SUCCESS;
	}
	
	
	

	@Override
	public String execute() {
		
//		测试赋值
//		this.openId="olWUvt186RJV4_obaeMV5SGT4e2g";
//		this.imgIds="dOPVnaoJ5bPhtmNLGW9YqtHAyward5gehriOP4yP3JAUOquXRzu3DuQPjJzOak3z";
//		this.cid=117;		
//		this.longitude="104.1674";
//		this.latitude="30.71578";
//		this.memo="";	
		
		this.tip="";
		this.openId=(this.openId==null?"":this.openId);
		this.type=(this.type==null?0:this.type);
		this.longitude=(this.longitude==null?"":this.longitude);
		this.latitude=(this.latitude==null?"":this.latitude);
		this.imgIds=(this.imgIds==null?"":this.imgIds);
		this.cid=(this.cid==null?0:this.cid);
		this.state=(this.state==null?"":this.state);
		this.name=(this.name==null?"":this.name);
		this.memo = (this.memo == null ? "" : this.memo.replaceAll("\\s*", ""));
		
		//System.out.println(this.state+"#####");
		
		if(this.openId.equals("")){		//这种情况一般不会出现
			this.tip="未识别到该用户";
			return "input";
		}
		
		User user=UserService.getInstance().getByOpenId(this.openId);
		
		if(user==null){					//这种情况一般不会出现
			this.tip="该用户还未注册";
			return ERROR;
		}
		
		if(this.cid==0){
			this.tip="请选择客户";
			return SUCCESS;
		}
		
		this.cb=ClientService.getInstance().getClientBaseDao().findByHql("from ClientBase c where c.cid="+this.cid).get(0);
		
		// check is owner
		if(!cb.getOwner().equals(user.getUid())){
//			System.out.println(cb.getOwner()+"|"+user.getUid());
//			this.tip="重新分配客户";
//			this.state="x";
			return "change_own";
		}
		
		
		
		boolean isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
		if(!isShow){
			this.tip="显示最近20条行动记录失败(code=003)";
			return ERROR;
		}
		
		this.monthTotal = ActivityService.getInstance().getCurrentMonthCount(user.getUid());
		
		
		if(this.state.equals("") && this.type!=9 && this.type!=11){	//except phoneActivity
			this.tip="请完善行动内容(行动类型|位置信息|照片|备注)";
			
			isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
			if(!isShow){
				
				return ERROR;
			}
			return SUCCESS;
		}
		
//		this.type=(this.type==null?0:this.type);	// repeat statement
		
		
		String tiXing="";
		
		if(this.state.equals("b")){					//如果是提交状态
		
			if(this.type==0)
				tiXing=tiXing+" 行动类型";
			if(this.type!=9 && this.type!=11){		// except phoneActivity
				if(this.latitude.equals("") || this.longitude.equals(""))
					tiXing=tiXing+" 位置信息";
				if(this.imgIds.equals(""))
					tiXing=tiXing+" 上传照片";
			}
			
			
			if(this.memo.equals(""))
				tiXing=tiXing+" 备注信息";
			
			if(!tiXing.equals("")){
				this.tip="创建失败！请完善行动内容：("+tiXing+" )";
				return ERROR;
			}
		
		
		
		Activity activity=new Activity();
		activity.setCid(this.cid);
		activity.setClientName(this.name);
		activity.setContent(this.memo);
		activity.setLatitude(this.latitude);
		activity.setLongitude(this.longitude);
		activity.setCreator(user.getUid());
		activity.setType(this.type);
		
		if(this.type!=9 && this.type!=11){	// not phone activity and 电话催收
			Image image=null;
			try{
			image=ImageService.getInstance().saveImageInfo(user.getUid(), this.imgIds, this.longitude, this.latitude,2);
			activity.setPath(image.getPath());
			activity.setCreated(image.getCreated());
			activity.setPhotoName(image.getFileName());
			activity.setAddress(image.getDiscription());
			}catch(Exception e){
				e.printStackTrace();
				this.tip="提交行动照片失败(code=002)";
				return ERROR;
			}
		}else
			activity.setCreated(TimeTool.getCurrentTime());
		
		
//		activity=ActivityService.getInstance().creatActvt(user.getUid(),imgIds,this.longitude,this.latitude);	//提交行动 包含了下载图片以及打水印
		
		try{
		ActivityService.getInstance().getActvtDao().merge(activity);
		ClientService.getInstance().updateLastTime(activity.getCid());	// 更新ClientBase中lastTime字段
		
		isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
		if(!isShow){
			
			return ERROR;
		}
		
		this.tip="提交行动成功";

		}catch(Exception e){
			e.printStackTrace();
			this.tip="提交行动失败(code=001)";
			return ERROR;
		}

		
		return SUCCESS;
		
		}
		return SUCCESS;
	}

	
	private boolean show(Integer creator,int maxCount){
		try{
			
			
			String begin=TimeTool.getTodayBeginStr();
			actvtList=ActivityService.getInstance().getActvtDao().findByPage("from Activity a where a.creator="+creator+" order by created desc",0,maxCount);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
			return true;
		}
	
	

	public static void main(String[] args){
		ActvtAction a=new ActvtAction();
		a.execute();
		System.out.println(a.getTip());
		System.out.println(a.getActvtList().get(0).getPhotoName());
	}
	

	public Integer getType() {
		return type;
	}



	public void setType(Integer type) {
		this.type = type;
	}



	public String getLongitude() {
		return longitude;
	}



	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}



	public String getLatitude() {
		return latitude;
	}



	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}



	public String getMemo() {
		return memo;
	}



	public void setMemo(String memo) {
		this.memo = memo;
	}



	public String getImgIds() {
		return imgIds;
	}



	public void setImgIds(String imgIds) {
		this.imgIds = imgIds;
	}



	



	public List<Activity> getActvtList() {
		return actvtList;
	}



	public void setActvtList(List<Activity> actvtList) {
		this.actvtList = actvtList;
	}



	public ClientDetail getCd() {
		return cd;
	}



	public void setCd(ClientDetail cd) {
		this.cd = cd;
	}



	public Integer getCid() {
		return cid;
	}



	public void setCid(Integer cid) {
		this.cid = cid;
	}


	public String getState() {
		return state;
	}


	public void setState(String state) {
		this.state = state;
	}



	public ClientBase getCb() {
		return cb;
	}



	public void setCb(ClientBase cb) {
		this.cb = cb;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public Integer getOppType() {
		return oppType;
	}



	public void setOppType(Integer oppType) {
		this.oppType = oppType;
	}






	public Integer getMonthTotal() {
		return monthTotal;
	}






	public void setMonthTotal(Integer monthTotal) {
		this.monthTotal = monthTotal;
	}

	public ActivityCount getActvCount() {
		return actvCount;
	}

	public void setActvCount(ActivityCount actvCount) {
		this.actvCount = actvCount;
	}

	
	
	

}
